<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: CommentService.cfc 1141 2007-05-01 03:46:30Z mpwoodward $

Notes:
--->
<cfcomponent displayname="CommentService" output="false" hint="CommentService for MachBlog">
	<cfproperty name="commentGateway" type="CommentGateway" required="true" />
	<cfproperty name="commentDAO" type="CommentDAO" required="true" />
	<cfproperty name="notificationService" type="org.machblog.notification.NotificationService" required="true" />
	<cfproperty name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
	<cfproperty name="udfs" type="org.machblog.utils.udfs" required="true" />
	
	<cffunction name="init" access="public" output="false" returntype="CommentService" 
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="setCommentGateway" access="public" output="false" returntype="void">
		<cfargument name="commentGateway" type="CommentGateway" required="true" />
		<cfset variables.commentGateway = arguments.commentGateway />
	</cffunction>
	<cffunction name="getCommentGateway" access="public" output="false" returntype="CommentGateway">
		<cfreturn variables.commentGateway />
	</cffunction>
	
	<cffunction name="setCommentDAO" access="public" output="false" returntype="void">
		<cfargument name="commentDAO" type="CommentDAO" required="true" />
		<cfset variables.commentDAO = arguments.commentDAO />
	</cffunction>
	<cffunction name="getCommentDAO" access="public" output="false" returntype="CommentDAO">
		<cfreturn variables.commentDAO />
	</cffunction>

	<cffunction name="setNotificationService" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="notificationService" type="org.machblog.notification.NotificationService" required="true" />
		<cfset variables.notificationService = arguments.notificationService />
	</cffunction>	
	<cffunction name="getNotificationService" access="private" output="false" returntype="org.machblog.notification.NotificationService">
		<cfreturn variables.notificationService />
	</cffunction>
	
	<cffunction name="setUserFacade" access="public" output="false" returntype="void">
		<cfargument name="userFacade" type="org.machblog.facades.UserFacade" 
				required="true" />
		<cfset variables.userFacade = arguments.userFacade />
	</cffunction>
	<cffunction name="getUserFacade" access="public" output="false" 
			returntype="org.machblog.facades.UserFacade">
		<cfreturn variables.userFacade />
	</cffunction>
	
	<cffunction name="setUdfs" access="public" output="false" returntype="void">
		<cfargument name="udfs" type="org.machblog.utils.udfs" required="true" />
		<cfset variables.udfs = arguments.udfs />
	</cffunction>
	<cffunction name="getUdfs" access="public" output="false" returntype="org.machblog.utils.udfs">
		<cfreturn variables.udfs />
	</cffunction>
	
	<cffunction name="getCommentBean" access="public" output="false" returntype="Comment">
		<cfreturn createObject("component", "Comment").init() />
	</cffunction>
	
	<!--- gateway methods --->
	<cffunction name="getCommentsByEntryId" access="public" output="false" returntype="query" 
			hint="Returns a query containing comments for an entry">
		<cfargument name="entryId" type="uuid" required="true" />
		<cfreturn getCommentGateway().getComments(entryId = arguments.entryId) />
	</cffunction>
	
	<cffunction name="getRecentComments" access="public" output="false" returntype="query" 
			hint="Returns a query containing recent comments across all entries">
		<cfargument name="numToGet" type="numeric" required="true" />
		<cfargument name="includeInactive" type="boolean" required="true" />
		<cfreturn getCommentGateway().getComments(arguments.numToGet, arguments.includeInactive) />
	</cffunction>
	
	<cffunction name="getAllComments" access="public" output="false" returntype="query" 
			hint="Returns a query containing all comments across all entries">
		<cfargument name="includeInactive" type="boolean" required="false" default="true">
		<cfargument name="searchTerms" type="string" required="false" default="" />
		<cfargument name="sortOrder" type="string" required="false" default="ASC" />
		<cfreturn getCommentGateway().getComments(includeInactive = arguments.includeInactive, 
													searchTerms = arguments.searchTerms, 
													sortOrder = arguments.sortOrder) />
	</cffunction>
	
	<cffunction name="getCommentCount" access="public" output="false" returntype="numeric" 
			hint="Returns the number of comments, optionally for a specific date.">
		<cfargument name="commentDate" type="date" required="false" default="#CreateDate(1900, 1, 1)#" />
		<cfreturn getCommentGateway().getCommentCount(arguments.commentDate) />
	</cffunction>
	
	<cffunction name="getSubscriptionCount" access="public" output="false" returntype="numeric" 
			hint="Returns the number of comment subscriptions across all comments">
		<cfreturn getCommentGateway().getSubscriptionCount() />
	</cffunction>
	
	<cffunction name="deactivateCommentsByEntryId" access="public" output="false" returntype="void" 
			hint="Deactivates comments for an entry">
		<cfargument name="entryId" type="uuid" required="true" />
		<cfset getCommentGateway().deactivateCommentsByEntryId(arguments.entryId) />
	</cffunction>
	
	<cffunction name="activateCommentsByEntryId" access="public" output="false" returntype="void" 
			hint="Activates comments for an entry">
		<cfargument name="entryId" type="uuid" required="true" />
		<cfset getCommentGateway().activateCommentsByEntryId(arguments.entryId) />
	</cffunction>
	
	<cffunction name="deleteCommentsByEntryId" access="public" output="false" returntype="void" 
			hint="Deletes comments for an entry">
		<cfargument name="entryId" type="uuid" required="true" />
		<cfset getCommentGateway().deleteCommentsByEntryId(arguments.entryId) />
	</cffunction>
	
	<cffunction name="toggleCommentDisplay" access="public" output="false" returntype="void" 
			hint="Toggles a comment display (show or hide)">
		<cfargument name="commentId" type="uuid" required="true" />
		<cfargument name="action" type="string" required="true" />
		
		<cfset getCommentGateway().toggleCommentDisplay(arguments.commentId, getUserFacade().getUser().getUserId(), arguments.action) />
	</cffunction>
	
	<cffunction name="toggleIpBlock" access="public" output="false" returntype="void" 
			hint="Adds or removes an ip address to/from the block list">
		<cfargument name="ipAddress" type="string" required="true" />
		<cfargument name="action" type="string" required="true" />
		
		<cfset getCommentGateway().toggleIpBlock(arguments.ipAddress, arguments.action, getUserFacade().getUser().getUserId()) />
	</cffunction>
	
	<cffunction name="deleteComments" access="public" output="false" returntype="void" 
			hint="Deletes comments">
		<cfargument name="deleteIds" type="string" required="true" />
		
		<cfset getCommentGateway().deleteComments(arguments.deleteIds) />
	</cffunction>
	
	<cffunction name="emailCommentNotification" access="public" output="false" returntype="void" 
			hint="Gets a list of subscribers to comments for a particular blog entry and notifies the subscribers as well as the admin if needed">
		<cfargument name="commentId" type="uuid" required="true" />
		<cfargument name="entryId" type="uuid" required="true" />
		<cfargument name="entryTitle" type="string" required="true" />
		<cfargument name="notifyAdmin" type="boolean" required="true" />
		<cfargument name="adminEmail" type="string" required="true" />
		<cfargument name="fromAddress" type="string" required="true" />
		<cfargument name="emailSubject" type="string" required="true" />
		<cfargument name="entryUrl" type="string" required="true" />
		<cfargument name="unsubscribeText" type="string" required="true" />
		<cfargument name="unsubscribeUrl" type="string" required="true" />
		<cfargument name="commentByText" type="string" required="true" />
		
		<cfset var subscribers = getCommentGateway().getCommentSubscribers(arguments.entryId) />
		<cfset var comment = getCommentBean().init(arguments.commentId) />
		
		<cfset getCommentDAO().read(comment) />
		
		<!--- if the comment is active send out any notifications that haven't been sent previously --->
		<cfif comment.getAudit().getIsActive() AND IsQuery(subscribers) AND subscribers.RecordCount GT 0>
			<cfloop query="subscribers">
				<cfif subscribers.email is not "">
					<cfset theUnsubscribeUrl = arguments.unsubscribeUrl & "&email=#subscribers.email#" />
					<cfset getNotificationService().emailCommentNotification(arguments.fromAddress, subscribers.email, 
																				arguments.emailSubject, arguments.entryUrl, 
																				arguments.unsubscribeText, 
																				arguments.unsubscribeUrl & "&email=#subscribers.email#", 
																				arguments.commentByText, 
																				comment.getName(), 
																				comment.getComment()) />
				</cfif>
			</cfloop>
		</cfif>

		<cfset getCommentGateway().updateEmailSentFlag(arguments.commentId) />
		
		<!--- notify admin if needed --->
		<cfif arguments.notifyAdmin>
			<cfset getNotificationService().emailCommentNotification(arguments.fromAddress, arguments.adminEmail, 
																		arguments.emailSubject, arguments.entryUrl, 
																		arguments.unsubscribeText, arguments.unsubscribeUrl, 
																		arguments.commentByText, comment.getName(), 
																		comment.getComment()) />
		</cfif>
	</cffunction>
	
	<cffunction name="unsubscribe" access="public" output="false" returntype="void" 
			hint="Unsubscribes the email address passed in from the entry based on the entry ID passed in">
		<cfargument name="entryId" type="uuid" required="true" />
		<cfargument name="email" type="string" required="true" />
		
		<cfset getCommentGateway().unsubscribe(arguments.entryId, arguments.email) />
	</cffunction>
	
	<!--- dao methods --->
	<cffunction name="getCommentById" access="public" output="false" returntype="Comment" hint="Retrieves a specific comment">
		<cfargument name="commentId" type="string" required="true" />
		
		<cfset var comment = getCommentBean().init(arguments.commentId) />
		
		<cfset getCommentDAO().read(comment) />
		<cfreturn comment />
	</cffunction>
	
	<cffunction name="createComment" access="public" output="false" returntype="void" hint="Creates a comment">
		<cfargument name="comment" type="Comment" required="true" />
		
		<!--- strip the html from the comment text and format the line breaks --->
		<cfset comment.setComment(Trim(getUdfs().XHTMLParagraphFormat(getUdfs().stripHTML(comment.getComment())))) />
		
		<cfset arguments.comment.setCommentId(CreateUUID()) />
		
		<cfset getCommentDAO().create(comment) />
	</cffunction>
	
	<cffunction name="updateComment" access="public" output="false" returntype="void" hint="Updates a comment">
		<cfargument name="comment" type="Comment" required="true" />
		<!--- strip the html from the comment text and format the line breaks --->
		<cfset comment.setComment(Trim(getUdfs().XHTMLParagraphFormat(getUdfs().stripHTML(comment.getComment())))) />
		<cfset getCommentDAO().update(comment) />
	</cffunction>
	
	<cffunction name="deleteComment" access="public" output="false" returntype="void" hint="Deletes a comment">
		<cfargument name="comment" type="Comment" required="true" />
		<cfset getCommentDAO().delete(comment) />
	</cffunction>
</cfcomponent>
